﻿using Ledict.Core.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ledict.ConditionExtract.DAL
{
    /// <summary>
    /// 备份数据库
    /// </summary>
    public class BackUpDataBaseDAL
    {
        public  void BackUp(string path)
        {
            try
            {
                string strSql = string.Format(@"               BACKUP DATABASE Ldt_JudgeDocSystem TO DISK= '{0}.bak';", path.Replace(".bak", ""));
                strSql = strSql.Replace("\\", "\\\\");
                Database oBase = DatabaseFactory.CreateDatabase("JudgeDoc");
                DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
                oCmd.CommandTimeout = 300;
                oBase.ExecuteNonQuery(oCmd);
                strSql = string.Format(@"  BACKUP DATABASE LdtCommonManage  TO DISK= '{0}1.bak';", path.Replace(".bak", ""));
                strSql = strSql.Replace("\\", "\\\\");
                Database oBase1 = DatabaseFactory.CreateDatabase("JudgeDoc");
                DbCommand oCmd1 = oBase1.GetSqlStringCommand(strSql.ToString());
                oCmd1.CommandTimeout = 300;
                oBase1.ExecuteNonQuery(oCmd1);
                strSql = string.Format(@" BACKUP DATABASE Ldt_JudgeProcess TO DISK= '{0}2.bak';",
                    path.Replace(".bak", ""));
                strSql = strSql.Replace("\\", "\\\\");
                Database oBase2 = DatabaseFactory.CreateDatabase("JudgeDoc");
                DbCommand oCmd2 = oBase.GetSqlStringCommand(strSql.ToString());
                oCmd2.CommandTimeout = 300;
                oBase2.ExecuteNonQuery(oCmd2);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("备份数据发生错误："+ex.Message);
            }
        }
    }
}
